﻿$(function () {
    MadronaTests_startUp();
    $('#load').click(function () {
        var modelName = $('#model').val();
        var recordId = $('#recordId').val();

        if (recordId)
            window.madronaTests.getRecord(modelName, recordId, null, null);
        else
            window.madronaTests.getModel(modelName, null, null);
    });
    $('#post').click(function () {
        var modelName = $('#model').val();
        var data = $('#results').val();
        //var recordId = $('#recordId').val();

        window.madronaTests.createRecord(modelName, data, null, null);
    });
    $('#delete').click(function () {
        var modelName = $('#model').val();
        var recordId = $('#recordId').val();

        if (recordId)
            window.madronaTests.deleteRecord(modelName, recordId, null, null);
        else
            alert('record id not specified!');
    });
});

function MadronaTests_startUp() {
    if (!window.madronaTests) window.madronaTests = {

        getModel: function (model, callback, callbackParams) {
            var request = jQuery.ajax({
                url: '/api/' + model + '/',
                success: function (msg) {
                    $('#results').val(JSON.stringify(msg));
                    if (callback) // only fire a callback if it has been specified
                        callback(msg, callbackParams);
                },
                error: function (jqXHR, textStatus) {
                    alert('Request failed: ' + textStatus);
                }
                //cache: false
                //type: "POST", // default = GET
                //data: { id: artworkId },
                //dataType: "html"
            });
        },
        getRecord: function (model, id, callback, callbackParams) {
            var request = jQuery.ajax({
                url: '/api/' + model + '/' + id,
                dataType: 'html',
                success: function (msg) {
                    $('#results').val(msg);
                    //$('#results').text(msg);//inconsistent behaviour with .text()
                    if (callback) // only fire a callback if it has been specified
                        callback(msg, callbackParams);
                },
                error: function (jqXHR, textStatus) {
                    alert('Request failed: ' + textStatus);
                }
                //cache: false
                //type: "POST", // default = GET
                //data: { id: artworkId },
                //dataType: "html"
            });
        },
        createRecord: function (model, data, callback, callbackParams) {
            var request = jQuery.ajax({
                type: "POST", // default = GET,
                url: '/api/' + model + '/',
                data: data,
                contentType: 'application/json',
                //dataType: 'html',
                success: function (msg) {
                    $('#results').val(JSON.stringify(msg));
                    if (callback) // only fire a callback if it has been specified
                        callback(msg, callbackParams);
                },
                error: function (jqXHR, textStatus) {
                    alert('Request failed: ' + textStatus);
                }
                //cache: false
                //dataType: "html"
            });
        },
        deleteRecord: function (model, id, callback, callbackParams) {
            var request = jQuery.ajax({
                type: "DELETE", // default = GET,
                url: '/api/' + model + '/' + id,
                //dataType: 'html',
                success: function (msg) {
                    $('#results').text(JSON.stringify(msg));
                    if (callback) // only fire a callback if it has been specified
                        callback(msg, callbackParams);
                },
                error: function (jqXHR, textStatus) {
                    alert('Request failed: ' + textStatus);
                }
                //cache: false
                //type: "POST", // default = GET
                //data: { id: artworkId },
                //dataType: "html"
            });
        }
    }
}